{include file="public/layout" /}

<body class="bodystyle" style="cursor: default; -moz-user-select: inherit;min-width:auto;">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
{include file="shop/left" /}
<div class="page min-hg-c-10">
    <div id="" class="explanation">
        <ul>
            <li>1、新增规格模板，方便管理员快捷选择商品规格复制到商品发布页里</li>
            <li>2、在规格库里编辑，修改，删除商品规格模板并<i style="color: red;">不影响</i>已发布的商品里的规格值</li>
            <li>3、如果编辑商品规格时需要和规格库里规格模板一致，可以点击规格值右侧的刷新按钮 <i class="fa fa-refresh"></i> 同步</li>
        </ul>
    </div>
    <div class="flexigrid mt10">
        <div class="hDiv">
            <div class="hDivBox">
                <table cellspacing="0" cellpadding="0" style="width: 100%">
                    <thead>
                    <tr>
                        <th class="sign w10" axis="col0">
                            <div class="tc"></div>
                        </th>
                        <th abbr="article_time" axis="col4" class="w180">
                            <div class="text-l10">规格名称</div>
                        </th>
                        <th abbr="ac_id" axis="col4">
                            <div class="text-l10">规格值</div>
                        </th>
                        <th abbr="article_time" axis="col6" class="w100">
                            <div class="tc">是否同步</div>
                        </th>
                        <th abbr="article_time" axis="col6" class="w60">
                            <div class="tc">排序</div>
                        </th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
        <div class="bDiv" style="height: auto;">
            <form id="PostForm">
                <div id="flexigrid" cellpadding="0" cellspacing="0" border="0">
                    <table style="width: 100%">
                        <input type="hidden" name="mark_preset_ids" id='mark_preset_ids'>
                        <input type="hidden" name="mark_mark_ids" id='mark_mark_ids'>
                        <tbody id='Template'>
                            {volist name="info" id="vo"}
                                <tr class="tr" id="tr_{$vo.preset_mark_id}">
                                    <td class="sign">
                                        <div class="w10 tc">
                                            <input type="hidden" name="preset_old[]" value="{$vo.preset_mark_id}">
                                        </div>
                                    </td>

                                    <td class="">
                                        <div class="w180 tc preset-bt3" style="text-align: left;">
                                            <span>
                                                <input type="text" name="preset_name_old_{$vo.preset_mark_id}[]" placeholder="请输入规格名称" onchange="MarkMarkId('{$vo.preset_mark_id}');" value="{$vo.preset_name}">
                                                <em data-id="tr_{$vo.preset_mark_id}" onclick="DelSpecTpl(this, '{$vo.preset_mark_id}')"><i style="position: absolute;right: 4px;" class="fa fa-times-circle" title="关闭"></i></em>
                                            </span>
                                        </div>
                                    </td>

                                    <td style="width: 100%">
                                        <div class="preset-bt3" style="text-align: left;" id="tr_td_input_old_{$vo.preset_mark_id}">
                                                {volist name="vo.preset_value" id="value"}
                                                    <span>
                                                        <input type="hidden" name="preset_id_old_{$vo.preset_mark_id}[]" value="{$value.preset_id}">
                                                        <input type="text" name="preset_value_old_{$vo.preset_mark_id}[]" placeholder="请输入规格值" onchange="MarkPresetId('{$value.preset_id}');" value="{$value.preset_value}"><em onclick="DelSpecTplValue(this, '{$value.preset_id}', '{$vo.preset_mark_id}')"><i  class="fa fa-times-circle" title="关闭"></i></em>
                                                    </span>
                                                {/volist}
                                                <span style="background:#4fc0e8; float: right; color: #fff; border-radius: 4px; padding:0px 6px  0px 20px; position: relative; cursor: pointer;" class="red" data-id="tr_td_input_old_{$vo.preset_mark_id}" onclick="AddSpecTplValue(this,'{$vo.preset_mark_id}','old');">
                                                    <i class="layui-icon layui-icon-addition" style="position:absolute; left: 0; top:0px; margin: 0 3px; color: #fff"></i>添加规格值
                                                </span>
                                        </div>
                                    </td>
                                    <td class="sort">
                                        <div class="w100 tc">
                                            <label class="label">
                                                <input class="check" type="checkbox" {eq name="$vo.spec_sync" value="1"} checked {/eq} value="1" name="spec_sync_{$vo.preset_mark_id}" onmouseover="layer_tips = layer.tips('勾选后，修改规格名称或规格值，会自动同步更新已发布的商品规格', this, {time:100000});" onmouseout="layer.close(layer_tips);">
                                            </label>
                                        </div>
                                    </td>
                                    <td class="sort">
                                        <div class="w60 tc">
                                            <input type="text" size="4" name="sort_order_{$vo.preset_mark_id}" value="{$vo.sort_order}">
                                        </div>
                                    </td>
                                </tr>
                            {/volist}    
                        </tbody>
                    </table>
                </div>
            </form>
            <div class="iDiv" style="display: none;"></div>
        </div>
        <div class="tDiv">
            <div class="tDiv2">
                <div class="fbutton">
                    <a href="JavaScript:void(0);" onclick="SaveSpecTpl(this);" class="layui-btn layui-btn-primary">
                            <span>保存</span>
                    </a>
                </div>
                <div class="fbutton">
                    <a href="JavaScript:void(0);" onclick="AddSpecTpl(this);" class="layui-btn layui-btn-primary">
                         <input type="hidden" id="PresetMarkId" value="{$PresetMarkId}">
                         <span class="red">新增规格</span>
                     </a>
                </div>
            </div>
            <div style="clear:both"></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    try{
        if (typeof(eval('is_conceal_1649209614'))=="function" && is_conceal_1649209614()){
            $(".page").css("margin-left","");
            $(".page").css("min-width","");
        }else{
            $(".page").css("margin-left","100px");
            $(".page").css("min-width","auto");
        }
    }catch(e){}

    $(document).ready(function(){
        // 表格行点击选中切换
        $('#flexigrid > table>tbody >tr').click(function(){
            $(this).toggleClass('trSelected');
        });

        // 点击刷新数据
        $('.fa-refresh').click(function(){
            location.href = location.href;
        });
    });

    // 追加规格模板
    function AddSpecTpl(obj){
        var PresetMarkId = $('#PresetMarkId').val();
        // 预设值标记ID，规格名称唯一标识
        if (!PresetMarkId || 0 == PresetMarkId) {
            // 标记ID出错则提示
            layer.alert('数据出错，请刷新重试~', {icon: 2, title:false});
        }
        // 拼装html
        var AddHtml = 
        [
            '<tr class="tr" id="tr_'+PresetMarkId+'">'+
                '<td class="sign">'+
                    '<div class="w10 tc">'+
                        '<input type="hidden" name="preset_new[]" value="'+PresetMarkId+'">'+
                        '<input type="hidden" name="preset_id_'+PresetMarkId+'[]">'+
                    '</div>'+
                '</td>'+

                '<td class="">'+
                    '<div class="w180 tc preset-bt3" style="text-align: left;">'+
                        '<span>'+
                        '<input type="text" name="preset_name_'+PresetMarkId+'[]" placeholder="请输入规格名称"><em data-id="tr_'+PresetMarkId+'" onclick="DelSpecTpl(this);"><i style="position: absolute;right: 4px;" class="fa fa-times-circle" title="关闭"></i></em>'+
                        '</span>'+
                    '</div>'+
                '</td>'+

                '<td style="width: 100%">'+
                    '<div class="preset-bt3" id="tr_td_input_'+PresetMarkId+'">'+
                            '<span>'+
                                '<input type="text" name="preset_value_'+PresetMarkId+'[]" placeholder="请输入规格值">'+
                                '<em onclick="DelSpecTplValue(this)"><i class="fa fa-times-circle" title="关闭"></i></em>'+
                            '</span>'+

                            '<span style="background:#4fc0e8; float:right; color: #fff; border-radius: 4px; padding:0px 6px  0px 20px; position: relative;" class="red" data-id="tr_td_input_'+PresetMarkId+'" onclick="AddSpecTplValue(this, '+PresetMarkId+');"><i class="layui-icon layui-icon-addition" style="position:absolute;left: 0; top:0px;margin: 0 3px; color: #fff"></i>添加规格值'+
                            '</span>'+
                    '</div>'+
                '</td>'+

                '<td class="sort">'+
                    '<div class="w100 tc">'+
                        '<label class="label">'+
                            '<input class="check" type="checkbox" value="0" disabled>'+
                        '</label>'+
                    '</div>'+
                '</td>'+

                '<td class="sort">'+
                    '<div class="w60 tc">'+
                        '<input type="text" name="sort_order_'+PresetMarkId+'" size="4" value="100">'+
                    '</div>'+
                '</td>'+
            '</tr>'
        ];

        // 追加规格名称规格值框架
        $('#Template').append(AddHtml);
        // 更新标记ID数
        PresetMarkId++;
        $('#PresetMarkId').val(PresetMarkId);
    }

    // 追加规格值
    function AddSpecTplValue(obj, PresetMarkId, type){
        var tr_td_input_id = $(obj).attr('data-id');
        var AddValue = [
            '<span>'
        ];
        if ('old' == type) {
            AddValue += [
                '<input type="hidden" name="preset_id_old_'+PresetMarkId+'[]">'+
                '<input type="text" name="preset_value_old_'+PresetMarkId+'[]" placeholder="请输入规格值">'+
                '<em onclick="DelSpecTplValue(this)"><i class="fa fa-times-circle" title="关闭"></i></em>'
            ];
        }else{
            AddValue += [
                '<input type="text" name="preset_value_'+PresetMarkId+'[]" placeholder="请输入规格值">'+
                '<em onclick="DelSpecTplValue(this)"><i class="fa fa-times-circle" title="关闭"></i></em>'
                
            ];
        }
        AddValue += [
            '</span>'
        ];
        $('#'+tr_td_input_id).append(AddValue);
    }

    // 删除规格模板
    function DelSpecTpl(obj, preset_mark_id){
        layer.confirm('确认删除规格名称？规格值将会一起删除', {
            shade: layer_shade,
            area: ['480px', '190px'],
            move: false,
            title: '提示',
            btnAlign:'r',
            closeBtn: 3,
            btn: ['确定', '取消'] ,//按钮
            success: function () {
                  $(".layui-layer-content").css('text-align', 'left');
              }
        }, function () {
            layer_loading('正在处理');
            if (preset_mark_id) {
                var url = "{:url('Shop/spec_delete')}";
                $.ajax({
                    type : 'post',
                    url  : url,
                    data : {preset_mark_id:preset_mark_id,_ajax:1},
                    dataType : 'json',
                    success : function(data){
                        layer.closeAll();
                        if(0 == data.code){
                            layer.alert(data.msg, {icon: 2, title:false});
                        }
                    }
                });
            }
            // 确定
            $('#'+$(obj).attr('data-id')).remove();
            layer.closeAll();
            layer.msg('操作成功！', {icon: 1, time: 1500});
            // 调用父级方法，更新预设规格下拉框的信息
            // if (preset_mark_id) parent.UpPresetSpecData(preset_mark_id);
        }, function (index) {
            // 取消
            layer.closeAll(index);
        });
    }

    // 删除指定规格值
    function DelSpecTplValue(obj, preset_id, preset_mark_id){
        layer_loading('正在处理');
        if (preset_id) {
            var url = "{:url('Shop/spec_delete')}";
            $.ajax({
                type : 'post',
                url  : url,
                data : {preset_id:preset_id,_ajax:1},
                dataType : 'json',
                success : function(data){
                    layer.closeAll();
                    if(0 == data.code){
                        layer.alert(data.msg, {icon: 2, title:false});
                    } else {
                        layer.msg(data.msg, {icon: 1, time:1000});
                        $(obj).parent().remove();
                        // parent.RefreshPresetValue(preset_id, preset_mark_id);
                    }
                }
            });
        } else {
            layer.closeAll();
            layer.msg('删除成功！', {icon: 1, time:1000});
            $(obj).parent().remove();
        }
    }

    // 保存规格信息
    function SaveSpecTpl(obj){
        layer_loading('正在处理');
        var mark_preset_ids = $('#mark_preset_ids').val();
        var mark_mark_ids = $('#mark_mark_ids').val();
        var url = "{:url('Shop/spec_template', ['_ajax'=>1])}";
        $.ajax({
            type : 'post',
            url  : url,
            data : $('#PostForm').serialize(),
            dataType : 'json',
            success : function(data){
                layer.closeAll();
                if (data.code == 1) {
                    // 关闭自身
                    // parent.UpPresetSpecData(null, mark_preset_ids, mark_mark_ids);
                    layer.closeAll();
                    layer.msg(data.msg, {icon: 1, time: 1500}, function() {
                        window.location.reload();
                    });
                    // 调用父级方法，更新预设规格下拉框的信息
                } else {
                    layer.alert(data.msg, {icon: 2, title: false});
                }
            }
        });
    }

    // 标记预设规格值ID
    function MarkPresetId(preset_id) {
        var mark_preset_ids = $('#mark_preset_ids').val();
        if (mark_preset_ids) {
            var mark_preset = mark_preset_ids.split(',');
            var is_mark = mark_preset.indexOf(preset_id);
            if (-1 == is_mark) {
                $('#mark_preset_ids').val(mark_preset_ids+','+preset_id);
            }
        }else{
            $('#mark_preset_ids').val(preset_id);
        }
    }

    // 标记预设规格名称ID
    function MarkMarkId(mark_id) {
        var mark_mark_ids = $('#mark_mark_ids').val();
        if (mark_mark_ids) {
            var mark_mark = mark_mark_ids.split(',');
            var is_mark = mark_mark.indexOf(mark_id);
            if (-1 == is_mark) {
                $('#mark_mark_ids').val(mark_mark_ids+','+mark_id);
            }
        }else{
            $('#mark_mark_ids').val(mark_id);
        }
    }
</script>
{include file="public/footer" /}